<?php

include_once $_SERVER['DOCUMENT_ROOT'].'/functions/password/bdd.php';

function dump($ignore)
{
 $server = 'localhost';
 $database = 'origavion';
 $user = 'origavion';
 $password = get_bdd_password();

 //Connexion à la base
 $db = mysql_connect($server, $user, $password) or die(mysql_error());
 mysql_select_db($database, $db) or die(mysql_error());

 //on récupère la liste des tables de la base de données
 //$tables = mysql_list_tables($database, $db) or die(mysql_error());
 $sql = 'SHOW TABLES FROM '.$database;
 $tables = mysql_query($sql) or die(mysql_error());

 // si on ne veut pas récupérer les $ignore premières tables
 for ($i=0; $i<$ignore; $i++) ($donnees = mysql_fetch_array($tables));

 // aller on boucle sur toutes les tables
 mkdir("./temp/".date('d-m-Y'));  
 while ($donnees = mysql_fetch_array($tables))
 {
  // on récupère le create table (structure de la table)
  $table = $donnees[0];
  $sql = 'SHOW CREATE TABLE '.$table;
  $res = mysql_query($sql) or die(mysql_error().$sql);
  if ($res)
  {  
   $backup_file = './temp/'.date('d-m-Y').'/backup_' . $table . '.sql.gz';
   $fp = gzopen($backup_file, 'w');

   $tableau = mysql_fetch_array($res);
   $tableau[1] .= ";\n";
   $insertions = $tableau[1];
   gzwrite($fp, $insertions);

   $req_table = mysql_query('SELECT * FROM '.$table) or die(mysql_error());
   $nbr_champs = mysql_num_fields($req_table);
   while ($ligne = mysql_fetch_array($req_table))
   {
    $insertions = 'INSERT INTO '.$table.' VALUES (';
    for ($i=0; $i<$nbr_champs; $i++)
    {
     $insertions .= '\'' . mysql_real_escape_string($ligne[$i]) . '\', ';
    }
    $insertions = substr($insertions, 0, -2);
    $insertions .= ");\n";
    gzwrite($fp, $insertions);
   }
  } // fin if ($res)
  mysql_free_result($res);
  gzclose($fp);
 }
 return true;
}
?>